#!/usr/bin/python

"""Project Euler Solution 023

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from euler.numbers.number_theory import AbundantNumbers
from itertools import takewhile

def get_answer(): 
    """Question:
    
    A perfect number is a number for which the sum of its proper divisors 
    is exactly equal to the number. For example, the sum of the proper divisors 
    of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect 
    number.

    A number n is called deficient if the sum of its proper divisors is less 
    than n and it is called abundant if this sum exceeds n.
    
    As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest 
    number that can be written as the sum of two abundant numbers is 24. By 
    mathematical analysis, it can be shown that all integers greater than 28123 
    can be written as the sum of two abundant numbers. However, this upper limit 
    cannot be reduced any further by analysis even though it is known that the 
    greatest number that cannot be expressed as the sum of two abundant numbers 
    is less than this limit.
    
    Find the sum of all the positive integers which cannot be written as the 
    sum of two abundant numbers.
    """    
    #The number below which all numbers will be tested for this problem.
    target_number = 28123
    
    #All the abundant number below the target number.    
    abundant_numbers = list(
                            takewhile(
                                      lambda n : n < target_number,
                                      AbundantNumbers()
                                    )
                        )

    #Mapping which indicates whether the number represented by the index
    #is the sum of two abundant numbers.
    number_is_abundant_sum_mapping = [False for n in xrange(target_number)] 

    #Mark those numbers which are not the sum of two abundant numbers.
    for n1 in takewhile(lambda n : n < target_number / 2, abundant_numbers):
        for n2 in takewhile(
                            lambda n : n < target_number - n1,
                            abundant_numbers
                        ):
            number_is_abundant_sum_mapping[n1 + n2] = True
    
    #Return result.
    return sum(
              n for n in xrange(28123) if not number_is_abundant_sum_mapping[n]
            )

if __name__ == "__main__":
    cProfile.run("print(get_answer())")
